package com.panomedic.utils;

import java.io.File;

public class SystemProperties {

    /**
    * Operating Systems Enum 
    */
    public enum OperatingSystem { WINDOWS, LINUX, MACOSX }

    /**
    * User home dir
    */
    public static final String userHome = System.getProperty("user.home");

    /**
    * SO dependent file separator
    */
    public static final String fileSeparator = System.getProperty("file.separator");

    /**
    * SO dependent line terminator
    */
    public static final String lineTerminator = getSystemLineTerminator();


    /**
    * Operating System
    */
    public static final OperatingSystem SYSTEM = detectOperatingSystem();

    private static String getSystemLineTerminator() 
    {
        if (SYSTEM == OperatingSystem.WINDOWS) 
            return "\r\n";
        return "\n";
    }

    /**
    * OS detection
    * @return
    */
    public static OperatingSystem detectOperatingSystem() 
    {
        String osName = System.getProperty("os.name").toLowerCase();
        
        if (osName.contains("windows")) {
            return OperatingSystem.WINDOWS;
        } else if (osName.contains("mac os x")) {
            return OperatingSystem.MACOSX;
        }
        
        return OperatingSystem.LINUX; 
    }
    
    /**
    * Gets folder where state is stored. If not exists, it's created
    * @param useWorkDir
    * @return
    */
    public static String getUserConfigFolder(boolean useWorkDir) 
    {
        if (useWorkDir)
            return ".";
        
        String userHomePath = SystemProperties.userHome;
        
        if (userHomePath != null) {
            File userConfigFolder = new File(userHomePath + fileSeparator + ".mse");
            
            if (!userConfigFolder.exists()) 
            {
                if (!userConfigFolder.mkdir())
                    return ".";
            }
            
            return userConfigFolder.getAbsolutePath();
        }
        
        return ".";
    }   
    
    // Create a directory and all non-existent ancestor directories 
    public static boolean createDir(File Directory) {
        return Directory.mkdirs();
    }

    // Create a directory and all non-existent ancestor directories 
    public static boolean createDir(String Directory) {
        return createDir(new File(Directory));
    }
    
    
}
